require([
    'jquery',
    'module/handlebars',
    'global',
    'underscore',
    'bootstrap.typeahead',
    'requirejs/domready!'], 
function($, handlebars, g, _, Typeahead) {
	
	var $form = $('#search-input').parents('form');
	
	var typeahead = new Typeahead('#search-input', {
		autoSelect: false,
		delay: 500,
		source: function (query, process) {
            $.ajax({
                url: g.context + '/search/tip',
                type: 'GET',
                dataType: 'json',
                data: {q: query, category: $form.find('input[name="category"]').val()},
                success: function(data) {
                    console.log(data);
                    process(data.data);
                }
            });
        },
        afterSelect: function(o){
        	if(!o) return;
        	var url = g.context;
        	if(o.type == 'vm'){
        		url += '/selection/' + o.id;
        	}else{
        		url += '/selection/' + o.parentId + '/products/' + o.id;
        	}
        	window.location.href = url;
        }
	});
	
});